﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using justice.Models;
using justice.ViewModel;

namespace justice.Controllers
{
    [Authorize]
    public class ShroffBookController : Controller
    {
        justiceEntities dal = new justiceEntities();
        public ActionResult shroffbookIndex(int? pageNum, string sewhere)
        {
            justice_user user = dal.justice_user.ToList().Find(l=>l.id == Convert.ToInt32(Session["uid"]));
            string where = "";

            if (user.insurer_id != null)
            {
                var dataa = from sb in dal.Shroff_book
                           join ju in dal.justice_user on sb.enter_user_id equals ju.id
                           join jc in dal.judiciary on ju.unit_id equals jc.id
                           join ir in dal.inserer on sb.insurer equals ir.insurer_id into left
                           from ir in left.DefaultIfEmpty()
                           where sb.insurer == user.insurer_id
                           select new shroffbooklist
                           {
                               shroffbook_id = sb.shroff_id,
                               tablenumber = sb.shroff_number,
                               time = sb.consignation_date,
                               judiciaryName = jc.judiciaryName,
                               insurerName = ir.insurerName
                           };
                Dictionary<List<shroffbooklist>, int[]> _list = Page.Page<shroffbooklist>.Data2(dataa.ToList(),pageNum);
                ViewBag.list = _list.ElementAt(0).Key;
                ViewBag.PageNum = _list.ElementAt(0).Value[0];
                ViewBag.PageSize = _list.ElementAt(0).Value[1];
                ViewBag.PageCount = _list.ElementAt(0).Value[2];
                return View(user);
            }

            if (user.unit_id == null && user.insurer_id == null)
            {
                where = "-";
            }
            else if (user.unit_id != null)
            {
                judiciary jud = dal.judiciary.ToList().Find(l=>l.id == user.unit_id);
                where = jud.abbreviation;
            }

            var data = from sb in dal.Shroff_book
                       join ju in dal.justice_user on sb.enter_user_id equals ju.id
                       join jc in dal.judiciary on ju.unit_id equals jc.id
                       join ir in dal.inserer on sb.insurer equals ir.insurer_id into left
                       from ir in left.DefaultIfEmpty()
                       where sb.shroff_number.Contains(@where)
                       select new shroffbooklist
                       {
                           shroffbook_id = sb.shroff_id,
                           tablenumber = sb.shroff_number,
                           time = sb.consignation_date,
                           judiciaryName = jc.judiciaryName,
                           insurerName = ir.insurerName
                       };
            Dictionary<List<shroffbooklist>, int[]> list = Page.Page<shroffbooklist>.Data2(data.ToList(), pageNum);
            ViewBag.list = list.ElementAt(0).Key;
            ViewBag.PageNum = list.ElementAt(0).Value[0];
            ViewBag.PageSize = list.ElementAt(0).Value[1];
            ViewBag.PageCount = list.ElementAt(0).Value[2];
            return View(user);
        }
        public ActionResult shroffbookinfos(int? id)
        {
            Shroff_book shk = dal.Shroff_book.ToList().Find(l=>l.shroff_id == id);
            be_appraiser beap = dal.be_appraiser.ToList().Find(l=>l.user_id == shk.main_case);
            inserer inser = dal.inserer.ToList().Find(l=>l.insurer_id == shk.insurer);
            nationality nation = dal.nationality.ToList().Find(l=>l.id == beap.nationality);
            shroffbookinfo shkinfo = new shroffbookinfo();
            shkinfo.shrbk = shk;
            shkinfo.beapp = beap;
            shkinfo.inserer = inser;
            shkinfo.narionality = nation;

            justice_user user = dal.justice_user.ToList().Find(l=>l.id == Convert.ToInt32(Session["uid"]));
            Role role = dal.Role.ToList().Find(l=>l.id == user.role_id);

            ViewBag.rolename = role.RoleName;
            ViewBag.user = user;
            ViewBag.id = id;

            return View(shkinfo);
        }
        public ActionResult ExportExcel(FormCollection form)
        {

            string strHtml = form["hHtml"];
            strHtml = HttpUtility.HtmlDecode(strHtml);//Html解码
            byte[] b = System.Text.Encoding.Default.GetBytes(strHtml);//字串转byte阵列
            return File(b, "application/vnd.ms-excel", "这是Excel.xls");//输出档案给Client端
        }
        public ActionResult Search(int? pageNum, string sewhere)
        {
            justice_user user = dal.justice_user.ToList().Find(l => l.id == Convert.ToInt32(Session["uid"]));
            string where = "";

            if (user.insurer_id != null)//保险公司身份
            {
                var dataa = from sb in dal.Shroff_book
                            join ju in dal.justice_user on sb.enter_user_id equals ju.id
                            join jc in dal.judiciary on ju.unit_id equals jc.id
                            join ir in dal.inserer on sb.insurer equals ir.insurer_id into left
                            from ir in left.DefaultIfEmpty()
                            where sb.insurer == user.insurer_id
                            select new shroffbooklist
                            {
                                shroffbook_id = sb.shroff_id,
                                tablenumber = sb.shroff_number,
                                time = sb.consignation_date,
                                judiciaryName = jc.judiciaryName,
                                insurerName = ir.insurerName
                            };
                if (sewhere != null)
                    dataa = dataa.Where(l => l.tablenumber.Contains(sewhere));
                Dictionary<List<shroffbooklist>, int[]> _list = Page.Page<shroffbooklist>.Data2(dataa.ToList(), pageNum);
                ViewBag.list = _list.ElementAt(0).Key;
                ViewBag.PageNum = _list.ElementAt(0).Value[0];
                ViewBag.PageSize = _list.ElementAt(0).Value[1];
                ViewBag.PageCount = _list.ElementAt(0).Value[2];
                return View(user);
            }

            if (user.unit_id == null && user.insurer_id == null)//超管身份
            {
                where = "-";
            }
            else if (user.unit_id != null)//鉴定机构身份
            {
                judiciary jud = dal.judiciary.ToList().Find(l => l.id == user.unit_id);
                where = jud.abbreviation;
            }

            var data1 = from sb in dal.Shroff_book
                        join ju in dal.justice_user on sb.enter_user_id equals ju.id
                        join jc in dal.judiciary on ju.unit_id equals jc.id
                        join ir in dal.inserer on sb.insurer equals ir.insurer_id into left
                        from ir in left.DefaultIfEmpty()
                        where sb.shroff_number.Contains(@where)
                        select new shroffbooklist
                        {
                            shroffbook_id = sb.shroff_id,
                            tablenumber = sb.shroff_number,
                            time = sb.consignation_date,
                            judiciaryName = jc.judiciaryName,
                            insurerName = ir.insurerName
                        };
            if (sewhere != null)
                data1 = data1.Where(l => l.tablenumber.Contains(sewhere));
            Dictionary<List<shroffbooklist>, int[]> list = Page.Page<shroffbooklist>.Data2(data1.ToList(), pageNum);
            ViewBag.list = list.ElementAt(0).Key;
            ViewBag.PageNum = list.ElementAt(0).Value[0];
            ViewBag.PageSize = list.ElementAt(0).Value[1];
            ViewBag.PageCount = list.ElementAt(0).Value[2];
            ViewBag.search = sewhere;
            return View("shroffbookIndex");
        }
    }
}